﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.Filters;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;
using GestionePolveriLib.Utilities;
using GestionePolveriLib.Facades.Extensions;
using FrameworkLib.Utilities;

namespace GestionePolveri2.Filters
{
    public partial class ElencoPrenotazioniFilter : ElencoPrenotazioniFilterBase
    {
        public ElencoPrenotazioniFilter()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                RefreshData();

                //    m_periodFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
                //    m_periodTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
                this.FromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
                this.ToDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
                this.UseDatesFilter = false;
                this.rbNonAnnullate.Checked = true;

                this.FilterBag.PropertyChanged += new PropertyChangedEventHandler(FilterBag_PropertyChanged);
            }
        }

        void FilterBag_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("codicepolvere", StringComparison.InvariantCultureIgnoreCase))
            {
                FrameworkLib.Utilities.ComboBoxHelper.SelectComboItem(this.cmbPolveri, this.FilterBag.CodicePolvere);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnResetFilter(EventArgs e)
        {
            this.cmbColori.SelectedIndex = -1;
            this.cmbPolveri.SelectedIndex = -1;
            this.txtOperatore.Clear();
            this.rbNonAnnullate.Checked = true;

            FilterBag.Status = PrenotazioneStatus.NonAnnullate;
            FilterBag.CodicePolvere = string.Empty;
            FilterBag.Operatore = string.Empty;
            FilterBag.CodiceColore = string.Empty;

            // particolare comportamento della cronologia che deve mostrare di default i movimenti del mese corrente...
            this.FromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
            this.ToDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
            this.UseDatesFilter = false;

            base.OnResetFilter(e);
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            Colore coloreSelected = FilterComboBoxHelper.GetSelectedItem<Colore>(this.cmbColori);
            Polvere polvereSelected = FilterComboBoxHelper.GetSelectedItem<Polvere>(this.cmbPolveri);

            FilterBag.Status = (this.rbAnnullate.Checked ? PrenotazioneStatus.Annullate :
                                                         (this.rbNonAnnullate.Checked ? PrenotazioneStatus.NonAnnullate : PrenotazioneStatus.Tutte));

            FilterBag.CodiceColore = (coloreSelected == null ? null : ((Colore)this.cmbColori.SelectedItem).CodiceColore);
            FilterBag.CodicePolvere = (polvereSelected == null ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.Operatore = (string.IsNullOrWhiteSpace(this.txtOperatore.Text) ? null : this.txtOperatore.Text);
            FilterBag.PeriodFrom = this.FromDate;
            FilterBag.PeriodTo = this.ToDate;
            FilterBag.UseDates = this.UseDatesFilter;


            if (
                (FilterBag.CodiceColore == null) &&
                (FilterBag.CodicePolvere == null) &&
                (FilterBag.Operatore == null) &&
                (FilterBag.UseDates == false) &&
                (FilterBag.Status == PrenotazioneStatus.Annullate)
              )
            {
                //OnResetFilter(e);
                base.OnExecuteFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);
        }

        private void LoadColori()
        {
            FilterComboBoxHelper.SetupColoriComboBox(this.cmbColori);
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadColori();
        }
    }

    public class ElencoPrenotazioniFilterBase : FilterContainerBase2<ElencoPrenotazioniFilterBag> { }
}
